package com.example.lib_common.base.internal

import android.R.attr.name
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

/**
 * 基础Fragment类，提供可见性回调
 * 包含以下回调方法：
 * - onFragmentShow(): 当Fragment对用户可见时调用
 * - onFragmentHide(): 当Fragment对用户不可见时调用
 * - onFragmentResume(): 当Fragment恢复时调用（结合可见性）
 * - onFragmentPause(): 当Fragment暂停时调用（结合可见性）
 */
open class InternalBaseFragment : Fragment(), IFragmentVisibility {

    // True if the fragment is visible to the user.
    private var mIsFragmentVisible = false

    // True if the fragment is visible to the user for the first time.
    private var mIsFragmentVisibleFirst = true

    override fun onStart() {
        super.onStart()
        determineFragmentVisible()
    }

    override fun onStop() {
        super.onStop()
        determineFragmentInvisible()
    }

    override fun onResume() {
        super.onResume()
        determineFragmentVisible()
    }

    override fun onPause() {
        super.onPause()
        determineFragmentInvisible()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden) {
            determineFragmentInvisible()
        } else {
            determineFragmentVisible()
        }
    }

    // 注意: setUserVisibleHint 已废弃，应考虑替代方案
    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            determineFragmentVisible()
        } else {
            determineFragmentInvisible()
        }
    }

    override fun isVisibleToUser(): Boolean = mIsFragmentVisible

    private fun determineFragmentVisible() {
        if (!shouldBeVisible()) return

        if (!mIsFragmentVisible) {
            mIsFragmentVisible = true
            onFragmentShow()
            if (mIsFragmentVisibleFirst) {
                mIsFragmentVisibleFirst = false
                onFragmentFirstShow()
            } else {
                onVisibleExceptFirst()
            }
            determineChildFragmentVisible()
        }
    }

    private fun determineFragmentInvisible() {
        if (mIsFragmentVisible) {
            mIsFragmentVisible = false
            onFragmentHide()
            determineChildFragmentInvisible()
        }
    }

    private fun shouldBeVisible(): Boolean {
        // 检查父Fragment是否可见
        val parent = parentFragment
        if (parent is InternalBaseFragment && !parent.isVisibleToUser()) {
            return false
        }

        // 检查当前Fragment是否满足可见条件
        return isResumed && !isHidden && userVisibleHint
    }

    private fun determineChildFragmentVisible() {
        childFragmentManager.fragments.forEach {
            if (it is InternalBaseFragment && it.shouldBeVisible()) {
                it.determineFragmentVisible()
            }
        }
    }

    private fun determineChildFragmentInvisible() {
        childFragmentManager.fragments.forEach {
            if (it is InternalBaseFragment) {
                it.determineFragmentInvisible()
            }
        }
    }
}